let { Service } = require('egg')
const { mysql } = require('../../config/plugin')
const getDateTime = require('../utils/dateTime')

class trendsController extends Service {
  // 发布发布公告、动态
  async sendTrends(title, authorId, content, coverPhoto,trendsId) {

    let { mysql } = this.app
    if (!coverPhoto) {
      coverPhoto = ""
    }
    let createTime = new Date().getTime()
    // 新增
    if(!trendsId){
      let res = await mysql.insert('trends', { title, authorId, content, coverPhoto,  createTime })
      if(res.affectedRows==1){
        return "发布成功"
      }else{
        return "发布失败"
      }
  
    }else{
      // 更新
      let options = {
        where:{
          trendsId
        }
      
      }
      let res = await mysql.update('trends',{title, authorId, content, coverPhoto,  createTime}, options)
      if(res.affectedRows==1){
        return "更新成功"
      }else{
        return "更新失败"
      }

    }
  


  }
  // 删除公告、动态
  async deleteTrends(trendsId) {
    let { mysql } = this.app
    let result = await mysql.delete('trends', { trendsId })
    if (result.affectedRows === 1) {
      return "删除成功"
    } else {
      return "删除失败"
    }


  }

//   增加阅览量
async readsAdd(trendsId){
    let { mysql } = this.app
    let trendsInfo = await mysql.get('trends',{trendsId})
    let reads_count = trendsInfo.reads_count+1
   let  options= {
        where:{
            trendsId
        }
    }
    let result = await mysql.update('trends',{reads_count},options)
    if(result.affectedRows===1){
        return "更新成功"
    }else{
        return "更新失败"
    }
}
  // 根据id获取公告、动态详情
  async trendsDetail(trendsId) {
    let { mysql } = this.app
    let result = await mysql.get('trends', { trendsId })
    // console.log(result);
    let obj = {}
    if (result) {
      obj = { ...result }
      obj.message = "获取成功"
      return obj
    } else {
      obj.message = "获取失败"
      return obj
    }
  }

  // 获取公告、动态列表  可传条件 标题 和  类别
  async trendsList(title, type,dateTime) {
    let { mysql } = this.app
    let obj = {}
    if (!title) {
      title = ""
    }

    let sql = "select * from trends where title like ? "
    let sql1 = "select count(*) total from  trends where title like ? "
    let result = await mysql.query(sql, ['%' + title + '%'])
    let total = await mysql.query(sql1, ['%' + title + '%'])


    if (type === undefined) {
      obj.list = result
      obj.total = total[0].total
  
    } else {
      type = parseInt(type)
      let arr = []
      let sum = 0
      result.forEach((item, index) => {
        if (item.type == type) {
          arr.push(item)
          sum++
        }
      })
      obj.list = arr
      obj.total = sum
    }
    if(dateTime){
      let arr = [...obj.list]
      let arr1 = []
      arr.forEach((item,index)=>{
        let dateTime1 = getDateTime(parseInt(item.createTime))
        if(dateTime==dateTime1){
          arr1.push(item)
        }
      })
      obj.list = [...arr1]
      obj.total = arr1.length
    }
    

    return obj

  }


}
module.exports = trendsController